package com.example.yls.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements IOnDelListener{
    private ListView stuListView;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private ArrayList<Student> SerachResultList = new ArrayList<>();
    private StuAdapter adapter;

    private Button btnAddStu;
    private EditText edtKeyword;
    private Button btnSearch;

    private boolean isResultList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        stuListView = findViewById(R.id.lv_stu);

        initStuArrayList();

        initAdapter();

        initListView();

        initAddBtn();

        initSerachView();
    }
    //初始化搜索按钮
    private void initSerachView() {
        edtKeyword=findViewById(R.id.edt_keyWord);
        btnSearch= findViewById(R.id.btn_search);

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               String keyWord = edtKeyword.getText().toString().trim();

               search(studentArrayList,keyWord);
               adapter.changeData(SerachResultList);

                isResultList = true;
            }
        });
        edtKeyword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String keyWord = edtKeyword.getText().toString().trim();

                search(studentArrayList,keyWord);
                adapter.changeData(SerachResultList);

            }
        });
    }

    /**
     * 使用学生列表实现关键字搜索
     * @param studentArrayList
     * @param keyWord
     */
    private void search(ArrayList<Student> studentArrayList, String keyWord) {
        SerachResultList.clear();
        for(int i =0;i<studentArrayList.size();i++){
            Student s = studentArrayList.get(i);

            if(s.getName().contains(keyWord)){
                SerachResultList.add(s);
            }
            else if(String.valueOf(s.getAge()).equals(keyWord)){
                SerachResultList.add(s);
            }
        }
    }

    //初始化添加学生按钮
    private void initAddBtn() {
        btnAddStu=findViewById(R.id.btn_AddStu);
        btnAddStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent= new Intent();
                intent.setClass(MainActivity.this,AddActivity.class);
                startActivityForResult(intent,1001);
            }
        });
    }

    //初始化listview
    private void initListView() {
        stuListView=findViewById(R.id.lv_stu);
        stuListView.setAdapter(adapter);



        stuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Student s =studentArrayList.get(i);
                        Toast.makeText(MainActivity.this,
                        "姓名"+s.getName() + " 年龄"
                                +  s.getAge(),
                        Toast.LENGTH_SHORT)
                        .show();
                Intent intent =new Intent();
                intent.setClass(MainActivity.this,EditActivity.class);

                intent.putExtra("NAME",s.getName());
                intent.putExtra("AGE",s.getAge());
                intent.putExtra("IMG",s.getImgId());
                intent.putExtra("INDEX",i);
                startActivityForResult(intent,1002);

            }
        });
    }

    //初始化适配器
    private void initAdapter() {
        adapter = new StuAdapter(MainActivity.this,
                studentArrayList,MainActivity.this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(data== null){
            return;
        }

        if(requestCode==1001 &&resultCode==2001 ){
        String name =data.getStringExtra("NAME");
        int age= data.getIntExtra("AGE",0);
        int imgId = data.getIntExtra("IMG",R.drawable.dog1);

        Student s= new Student(name,age,imgId);
        studentArrayList.add(s);
        adapter.notifyDataSetChanged();
        }
        else if(resultCode==2002){
            String name =data.getStringExtra("NAME");
            int age= data.getIntExtra("AGE",0);
            int imgId = data.getIntExtra("IMG",R.drawable.dog1);
            int index= data.getIntExtra("INDEX",0);

            Student s= studentArrayList.get(index);
            s.setName(name);
            s.setAge(age);
            s.setImgId(imgId);

            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 初始化学生列表的数据
     */
    private void initStuArrayList() {
        Student s1 = new Student("张一", 18, R.drawable.dog1);
        Student s2 = new Student("张二", 19, R.drawable.dog2);
        Student s3 = new Student("张三", 20, R.drawable.dog3);
        Student s4 = new Student("张四", 21, R.drawable.dog4);
        Student s5 = new Student("张五", 22, R.drawable.dog5);

        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);

    }

    @Override
    public void del(int i) {
        studentArrayList.remove(i);
        adapter.notifyDataSetChanged();
    }
}
